package com.apollo.android.um.imp;

import android.content.Context;

import com.apollo.android.um.anno.notice.event.CustomAction;
import com.apollo.android.um.anno.notice.event.LaunchApp;
import com.apollo.android.um.anno.notice.event.OpenActivity;
import com.apollo.android.um.anno.notice.event.OpenUrl;
import com.apollo.android.um.constants.Constants;
import com.apollo.android.um.utils.LogUtils;
import com.apollo.android.utils.Helper;
import com.apollo.android.utils.ResUtils;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import java.lang.reflect.Method;

public class UNotificationClickHandlerImp extends UmengNotificationClickHandler {
    private Method launchAppMethod;
    private Method openUrlMethod;
    private Method openActivityMethod;
    private Method customActionMethod;

    private boolean launchApp(Method method, Context context, UMessage uMessage) {
        try {
            LaunchApp launchApp = method.getAnnotation(LaunchApp.class);
            if (launchApp != null) {
                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes != null && paramTypes.length == 2 && paramTypes[0] == Context.class && paramTypes[1] == UMessage.class) {
                    method.invoke(null, context, uMessage);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void launchApp(Context context, UMessage uMessage) {
        LogUtils.log("launchApp");
        if (launchAppMethod != null && launchApp(launchAppMethod, context, uMessage)) {
            super.launchApp(context, uMessage);
            return;
        }
        launchAppMethod = null;
        final String registerClassName = ResUtils.getString(Constants.CLASS_MESSAGE_EVENT);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        if (launchApp(method, context, uMessage)) {
                            launchAppMethod = method;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.launchApp(context, uMessage);
    }

    private boolean openUrl(Method method, Context context, UMessage uMessage) {
        try {
            OpenUrl openUrl = method.getAnnotation(OpenUrl.class);
            if (openUrl != null) {
                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes != null && paramTypes.length == 2 && paramTypes[0] == Context.class && paramTypes[1] == UMessage.class) {
                    method.invoke(null, context, uMessage);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void openUrl(Context context, UMessage uMessage) {
        LogUtils.log("openUrl");
        if (openUrlMethod != null && openUrl(openUrlMethod, context, uMessage)) {
            super.openUrl(context, uMessage);
            return;
        }
        openUrlMethod = null;
        final String registerClassName = ResUtils.getString(Constants.CLASS_MESSAGE_EVENT);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        if (openUrl(method, context, uMessage)) {
                            openUrlMethod = method;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.openUrl(context, uMessage);
    }

    private boolean openActivity(Method method, Context context, UMessage uMessage) {
        try {
            OpenActivity openActivity = method.getAnnotation(OpenActivity.class);
            if (openActivity != null) {
                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes != null && paramTypes.length == 2 && paramTypes[0] == Context.class && paramTypes[1] == UMessage.class) {
                    method.invoke(null, context, uMessage);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void openActivity(Context context, UMessage uMessage) {
        LogUtils.log("openActivity");
        if (openActivityMethod != null && openActivity(openActivityMethod, context, uMessage)) {
            return;
        }
        openActivityMethod = null;
        final String registerClassName = ResUtils.getString(Constants.CLASS_MESSAGE_EVENT);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        if (openActivity(method, context, uMessage)) {
                            openActivityMethod = method;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean dealWithCustomAction(Method method, Context context, UMessage uMessage) {
        try {
            CustomAction customAction = method.getAnnotation(CustomAction.class);
            if (customAction != null) {
                Class[] paramTypes = method.getParameterTypes();
                if (paramTypes != null && paramTypes.length == 2 && paramTypes[0] == Context.class && paramTypes[1] == UMessage.class) {
                    method.invoke(null, context, uMessage);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void dealWithCustomAction(Context context, UMessage uMessage) {
        LogUtils.log("dealWithCustomAction");
        if (customActionMethod != null && dealWithCustomAction(customActionMethod, context, uMessage)) {
            return;
        }
        customActionMethod = null;
        final String registerClassName = ResUtils.getString(Constants.CLASS_MESSAGE_EVENT);
        if (Helper.isNotEmpty(registerClassName)) {
            try {
                Class registerClass = Class.forName(registerClassName);
                Method[] methods = registerClass.getDeclaredMethods();
                if (methods != null) {
                    for (Method method : methods) {
                        if (dealWithCustomAction(method, context, uMessage)) {
                            customActionMethod = method;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
